package com.yukino.controller;

import com.yukino.pojo.requestBody.AddProject;
import com.yukino.pojo.requestBody.ProjectSearchCondition;
import com.yukino.pojo.requestBody.UpdateProject;
import com.yukino.service.ProjectService;
import com.yukino.util.AjaxJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    /**
     * 获取所有项目信息
     */
    @GetMapping("/project/list")
    public AjaxJson queryProject() {
        return AjaxJson.getSuccessData(projectService.queryInfo());
    }

    /**
     * 条件搜索项目
     * @param condition 条件
     */
    @GetMapping("/project/list/condition")
    public AjaxJson queryProjectCondition(@RequestBody ProjectSearchCondition condition) {
        String project_name = condition.getProject_name();
        String dept_id = condition.getDept_id();
        String leader = condition.getLeader();
        Map<String, String> map = new HashMap<>();

        if (!project_name.equals("")) map.put("project_name", project_name);
        if (!dept_id.equals("")) map.put("dept_id", dept_id);
        if (!leader.equals("")) map.put("leader", leader);

        System.out.println("111"+map);
        return AjaxJson.getSuccessData(projectService.queryInfoByCondition(map));
    }

    /**
     * 新增一个项目
     * @param project 新增项目的基本信息
     */
    @PostMapping("/project/add")
    public AjaxJson addOne(@RequestBody AddProject project) {
        try {
            projectService.addOne(project);
            return AjaxJson.getSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxJson.getError("增加失败");
        }
    }

    /**
     * 删除一个项目
     * @param project_id 项目的唯一标识id
     */
    @DeleteMapping("/project/delete/{project_id}")
    public AjaxJson deleteInfo(@PathVariable("project_id") String project_id){
        projectService.deleteOne(project_id);
        return AjaxJson.getSuccess();
    }

    /**
     * 更新项目信息
     * @param project 更新的项目信息
     * @return
     */
    @PutMapping("/project/update")
    public AjaxJson updateInfo(@RequestBody UpdateProject project){
        projectService.updateOne(project);
        return AjaxJson.getSuccess();
    }
}
